<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8" style="margin-top:20px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          v-hasPermi="['marketing:promoter:edit']"
          @click="handleAdd"
          >添加推广人</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList">
      <el-table-column width="70" type="index" label="序号" align="center" />
      <el-table-column label="账号" align="center" prop="nickname" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="电话" align="center" prop="nickname">
        <template slot-scope="scope">
          <!-- {{ typeof postList[scope.$index].nickname }} -->
          <p v-if="typeof postList[scope.$index].nickname === 'number'">
            {{ postList[scope.$index].nickname }}
          </p>
        </template>
      </el-table-column>
      <el-table-column label="单位" align="center" prop="station" />
      <el-table-column
        label="二维码"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="getParticulars(scope.row)"
            v-hasPermi="['marketing:promoter:edit']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['marketing:promoter:edit']"
            @click="download(scope.row)"
            >下载</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="推广新用户" align="center" prop="stationcode" />
      <el-table-column label="推广订单" align="center" prop="stationlevel" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="getDetails(scope.row)"
            v-hasPermi="['marketing:promoter:edit']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['marketing:promoter:edit']"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['marketing:promoter:edit']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改推广人 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="18">
            <el-form-item
              label="推广人账号:"
              prop="nickname"
              label-width="115px"
            >
              <el-input v-model="form.nickname" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="密码:" prop="password" label-width="115px">
              <el-input v-model="form.password" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="姓名:" prop="name" label-width="115px">
              <el-input v-model="form.name" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item
              label="单位编码:"
              prop="organcode"
              label-width="115px"
            >
              <el-input v-model="form.organcode" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="单位:" prop="station" label-width="115px">
              <el-input v-model="form.station" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="二维码:" prop="qrurl" label-width="115px">
              <el-input v-model="form.qrurl" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="compile" @click="getSubmit"
          >确 定</el-button
        >
        <el-button type="primary" v-if="add" @click="SubmitForm"
          >确 定</el-button
        >

        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查看二维码 -->
    <el-dialog
      :title="title"
      :visible.sync="particulars"
      width="500px"
      append-to-body
    >
      <el-form ref="form" label-width="80px">
        <el-row>
          <img style="width:100%" :src="imageUrl" alt="" />
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="particulars = false">确 定</el-button>
        <el-button @click="particulars = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 删除 -->
    <el-dialog
      :title="title"
      :visible.sync="configuration"
      width="300px"
      append-to-body
    >
      <el-form ref="form" label-width="80px">
        <el-row>
          <el-col>
            确定删除这个推广人吗？
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="getSubmitForm">确 定</el-button>
        <el-button @click="configuration = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  list,
  downloadQr,
  MarketingPerson,
  modification,
  delById
} from "@/api/marketing/promoter";
import elementIcons from "../../components/icons/element-icons";
export default {
  name: "Promoter",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //查看二维码
      particulars: false,
      //删除弹出框
      configuration: false,
      // 二维码图片
      imageUrl: "",
      //新增请求
      add: false,
      //编辑请求
      compile: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      //当前行id
      id: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nickname: [
          { required: true, message: "推广人账号不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        organcode: [
          { required: true, message: "单位编码不能为空", trigger: "blur" }
        ],
        station: [
          { required: true, message: "单位名称不能为空", trigger: "blur" }
        ],
        qrurl: [{ required: true, message: "二维码不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList(); //推广人列表
  },
  methods: {
    /** 查询推广人列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then(response => {
        if (response.code == 200) {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        } else {
          this.$message.error(response.msg);
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.particulars = false;
      this.configuration = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: "",
        nickname: "",
        password: "",
        name: "",
        organcode: "",
        station: "",
        qrurl: ""
      };
    },
    /** 添加推广人按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.compile = false;
      this.title = "添加推广人";
      this.add = true;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.compile = true;
      this.add = false;
      this.form = row;
      this.form.id = row.id;
      this.title = "修改推广人";
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    //新增请求
    SubmitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          MarketingPerson(this.form).then(response => {
            if (response.code == 200) {
              this.open = false;
              this.getList();
              this.$message({
                message: "添加成功",
                type: "success"
              });
            } else {
              this.$message.error(response.msg);
            }
          });
        }
      });
    },
    // 编辑请求
    getSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          modification(this.form).then(response => {
            if (response.code == 200) {
              this.open = false;
              this.getList();
            } else {
              this.$message.error(response.msg);
            }
          });
        }
      });
    },
    //查看二维码
    getParticulars(row) {
      this.title = "查看二维码";
      downloadQr({ id: row.id }).then(response => {
        if (response.type == "text/xml") {
          this.particulars = true;
          const blob = new Blob([response]);
          const url = window.URL.createObjectURL(blob);
          this.imageUrl = url;
        } else {
          this.$message.error("没有二维码");
        }
      });
    },
    // 下载二维码
    download(row) {
      downloadQr({ id: row.id }).then(response => {
        console.log(response.type);
        if (response.type == "text/xml") {
          const blobs = new Blob([response]);
          let a = document.createElement("a");
          let objectUrl = URL.createObjectURL(blobs);
          a.setAttribute("href", objectUrl);
          a.setAttribute("download", "二维码.png");
          a.click();
        } else {
          this.$message.error("没有二维码");
        }
      });
    },
    // 跳转到推广人详情页面
    getDetails(row) {
      this.$router.push({
        path: `./promoterDetails`,
        query: {
          id: row.id
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.title = "删除";
      this.configuration = true;
      this.id = row.id;
    },
    //  删除
    getSubmitForm(val) {
      delById({ id: this.id }).then(response => {
        if (response.code == 200) {
          this.getList();
          this.configuration = false;
          this.$message({
            message: "删除成功",
            type: "success"
          });
        } else {
          this.$message.error(response.msg);
        }
      });
    }
  }
};
</script>
